﻿using CPPEI.Coolzon.UI.WinForm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;

namespace CPPEI.Coolzon.UI
{
    public abstract class ActiveXPart : ControlPart
    {
        static ActiveXPart()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ActiveXPart), new FrameworkPropertyMetadata(typeof(ActiveXPart)));

        }

        protected ActiveXWrapper _activeXWrapper;
        protected System.Windows.Forms.Integration.WindowsFormsHost _winHost;

        /// <summary>
        /// ActiveXWrapper的投影图像
        /// </summary>
        public ImageSource WrapperProjectionImageSource
        {
            get
            {
                return (ImageSource)GetValue(WrapperProjectionImageSourceProperty);
            }
            set
            {
                SetValue(WrapperProjectionImageSourceProperty, value);
            }
        }

        public static DependencyProperty WrapperProjectionImageSourceProperty = DependencyProperty.Register("WrapperProjectionImageSource",
            typeof(ImageSource),
            typeof(ActiveXPart),
            null);

        /// <summary>
        /// 是否冻结
        /// </summary>
        public bool XIsFrozen
        {
            get
            {
                return (bool)GetValue(XIsFrozenProperty);
            }
            set
            {
                SetValue(XIsFrozenProperty, value);
            }
        }
        public static DependencyProperty XIsFrozenProperty = DependencyProperty.Register("XIsFrozen",
            typeof(bool),
            typeof(ActiveXPart),
            null);

        public bool IsShowActiveX
        {
            get
            {
                return (bool)GetValue(IsShowActiveXProperty);
            }
            set
            {
                SetValue(IsShowActiveXProperty, value);
            }
        }
        public static DependencyProperty IsShowActiveXProperty = DependencyProperty.Register("IsShowActiveX",
            typeof(bool),
            typeof(ActiveXPart),
            null);

        public ActiveXPart()
        {
            XIsFrozen = false;
            IsShowActiveX = true;
        }

        protected void SetActiveXWrapper(System.Windows.Forms.Integration.WindowsFormsHost winHost, ActiveXWrapper activeXWrapper)
        {
            _activeXWrapper = activeXWrapper;
            _winHost = winHost;
            if (_activeXWrapper != null)
            {
                Common.Logger.DebugToTag("UI", "ActiveXPart:{0} set content size:{1},{2}", this.Name, this.XWidth, this.XHeight);
                _activeXWrapper.SetContentSize(this.XWidth, this.XHeight);
            }
        }

        protected override void OnInit(PartContext context)
        {
            IsShowActiveX = context.IsShowActiveXPart;
            if (context.IsShowActiveXPart)
            {
                Coolzon.Common.Logger.InfoToTag("ActiveXPart", "初始化ActiveX组件Type：{0}", this.GetType().FullName);
                OnActiveXInit(context);
            }
            base.OnInit(context);
        }

        protected override void OnActive(PartContext context)
        {
            IsShowActiveX = context.IsShowActiveXPart;
            if (context.IsShowActiveXPart)
            {
                OnActiveXActive(context);
            }
            base.OnActive(context);
        }

        protected override void OnDeactive(PartContext context)
        {
            if (context.IsShowActiveXPart)
            {
                OnActiveXDeactive(context);
            }
            base.OnDeactive(context);
        }

        protected override void OnDestory()
        {
            base.OnDestory();
        }

        protected override void OnVisibleRegionChanged(System.Windows.Rect rect)
        {
            //Common.Logger.DebugToTag("UI", "ActiveXPart:{0} onVisibleRegionChanged rect:{1}", this.Name, rect);
            if (_winHost != null)
            {
                _winHost.Margin = new System.Windows.Thickness(rect.Left, rect.Top, 0, 0);
                _winHost.Width = rect.Width;
                _winHost.Height = rect.Height;

                if (_activeXWrapper != null)
                {
                    _activeXWrapper.SetWrapperSize(rect.Width, rect.Height);

                    //Common.Logger.DebugToTag("UI", "ActiveXPart:{0} set content size:{1},{2}", this.Name, this.XWidth, this.XHeight);
                    //_activeXWrapper.SetContentSize(this.XWidth, this.XHeight);

                    //Common.Logger.DebugToTag("UI", "ActiveXPart:{0} set content scroll offset {1},{2}", this.Name, rect.Left, rect.Top);
                    _activeXWrapper.SetContentScrollOffset(rect.Left, rect.Top);
                }

            }
        }

        protected override void OnFrozen()
        {
            XIsFrozen = true;

            if (this._activeXWrapper != null)
            {
                System.Drawing.Bitmap bitmap = this._activeXWrapper.ProjectionToImage();
                WrapperProjectionImageSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                    bitmap.GetHbitmap(),
                    IntPtr.Zero,
                    Int32Rect.Empty,
                    System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
                Common.Logger.DebugToTag("UI", "ActiveX:{0} onfrozen 获取wrapper图像w:{1} h:{2}", this.Name, bitmap.Width, bitmap.Height);
            }

            base.OnFrozen();
        }

        protected override void OnUnfreeze()
        {
            XIsFrozen = false;

            base.OnUnfreeze();
        }

        protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
        {
            base.OnRenderSizeChanged(sizeInfo);
        }

        protected abstract void OnActiveXInit(PartContext context);

        protected abstract void OnActiveXActive(PartContext context);

        protected abstract void OnActiveXDeactive(PartContext context);
    }
}
